package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.server.mapper.MenuRoleMapper;
import com.xxxx.server.pojo.AdminRole;
import com.xxxx.server.pojo.MenuRole;
import com.xxxx.server.service.IMenuRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-08-17
 */
@Service
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements IMenuRoleService {

    @Resource
    private MenuRoleMapper menuRoleMapper;
    /**
     * 更新用户资源
     * @param rid
     * @param mids
     */
    @Override
    public void updateAdminRole(Integer rid, String mids) {
        // 判断用户是否有资源
        Integer count = menuRoleMapper.countMenuRoleByrid(rid);

        // 如果存在用户资源，则先删除
        if (count > 0) {
            menuRoleMapper.deleteMenuRoleByrid(rid);
        }

        // 判断资源id为空
        if (mids != null) {
            // 用户资源集合
            List<MenuRole> menuRoles = new ArrayList<>();

            // 将字符串转化为数组
            String[] menuIdArray = mids.split(",");

            // 遍历资源id，封装资源角色对象
            for (String menuId : menuIdArray) {
                MenuRole menuRole = new MenuRole();

                menuRole.setRid(rid);
                menuRole.setMid(Integer.parseInt(menuId));

                menuRoles.add(menuRole);
            }

            MenuRoleServiceImpl menuRoleService = new MenuRoleServiceImpl();

            menuRoleService.saveBatch(menuRoles);
        }
    }
}
